# coding=utf-8
# Copyright 2024 The Google Research Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""tests for hash_encoding.py."""

import jax
import jax.numpy as jnp
import pytest
from smerf.internal import hash_encoding


@pytest.mark.parametrize(
    "cls",
    [hash_encoding.MultiPropHashEncoding, hash_encoding.MultiHashEncoding],
)
def test_multi_hash_encoding(cls):
  b = 8 * 20
  k = 16

  model = cls(hash_map_size=2**5)

  # Initialize model.
  idxs = jnp.mod(jnp.arange(b).reshape((b // k, k, 1)), 64)
  xs = jnp.zeros((b // k, k, 3))
  rng = jax.random.PRNGKey(0)
  params = jax.jit(model.init)(rng, idxs[0:1], xs[0:1])

  # Apply model.
  model_apply_jit = jax.jit(model.apply)
  y = model_apply_jit(params, idxs, xs)

  # Verify output shape.
  expected_shape = (b // k, k, model.num_features * model.num_scales)
  assert y.shape == expected_shape
